﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using Widgetsphere.Generator.Common.GeneratorFramework;
using Widgetsphere.Generator.Models;
using Widgetsphere.Generator.ProjectItemGenerators;
using Widgetsphere.Generator.DataTransfer;

namespace Acme.GeneratorExtension.GenerateToDataTransfer
{
	/// <summary>
	/// This generator will generate the [EntityName]MetaData.cs file. This file 
	/// contains a public partial class [EntityName]MetaData. For the generated
	/// file the class will be empty. This file is provided as a way to allow programmers to
	/// extend the generated class. It will be generated once and never over written after that 
	/// point.
	/// </summary>
	[GeneratorItemAttribute("MetaData Class Extension", typeof(DataTransferProjectGenerator))]
	public class MetaDataInfoClassExtensionGenerator : BaseGenerator
	{
		#region Class Members
		private const string RELATIVE_OUTPUT_LOCATION = @"\MetaData\";
		#endregion

		#region Overrides
		/// <summary>
		/// Number of files to be generated by the system
		/// </summary>
		public override int FileCount
		{
			//return the number of tables that are marked to be generated
			get { return _model.Database.Tables.Count(x => x.Generated); }
		}

		/// <summary>
		/// Method called by the core generator. This method is used to 
		/// manage what items are to be generated by the system. 
		/// </summary>
		public override void Generate()
		{
			//Walk through all the tables in the model. 
			//Generate classes for those that are specified to be generated.
			foreach (Table table in _model.Database.Tables.Where(x => x.Generated).OrderBy(x => x.Name))
			{
				//Construct the template class passing it the current model file and table
				MetadataInfoClassExtensionTemplate template = new MetadataInfoClassExtensionTemplate(_model, table);

				//Create a string that represents the project relative output location of the file (Ex: \MetaData\Customer.cs)
				string projectRelativeFileName = RELATIVE_OUTPUT_LOCATION + template.FileName;

				//Get the generated file content.
				string fileContent = template.FileContent;

				//Publish the Project Item Generated event for the generation engine to handle
				ProjectItemGeneratedEventArgs eventArgs =
					new ProjectItemGeneratedEventArgs(projectRelativeFileName, fileContent, ProjectName, this, false);

				OnProjectItemGenerated(this, eventArgs);
			}

			//Publish the generation completed event to identify to the engine all files have been generated.
			ProjectItemGenerationCompleteEventArgs gcEventArgs = new ProjectItemGenerationCompleteEventArgs(this);
			OnGenerationComplete(this, gcEventArgs);
		}

		/// <summary>
		/// This is the name of the project. For this example it is the models 
		/// company name and project name pre-pended to data transfer
		/// </summary>
		protected override string ProjectName
		{
			get
			{
				ModelRoot modelRoot = ((ModelRoot)_model);
				return string.Format("{1}.{0}.DataTransfer", modelRoot.ProjectName, modelRoot.CompanyName);
			}
		}
		#endregion

	}
}